<template>
  <div class="task-create-container">
    <div class="page-header">
      <h1>创建备课任务</h1>
    </div>

    <el-form :model="taskForm" label-position="top" class="task-form">
      <el-form-item label="课程名称" required>
        <el-input v-model="taskForm.title" placeholder="请输入课程名称"></el-input>
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="学科" required>
            <el-select v-model="taskForm.subject" placeholder="请选择学科">
              <el-option label="语文" value="chinese"></el-option>
              <el-option label="数学" value="math"></el-option>
              <el-option label="英语" value="english"></el-option>
              <el-option label="物理" value="physics"></el-option>
              <el-option label="化学" value="chemistry"></el-option>
              <el-option label="生物" value="biology"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年级" required>
            <el-select v-model="taskForm.grade" placeholder="请选择年级">
              <el-option label="七年级" value="7"></el-option>
              <el-option label="八年级" value="8"></el-option>
              <el-option label="九年级" value="9"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="课程描述">
        <el-input
          v-model="taskForm.description"
          type="textarea"
          :rows="4"
          placeholder="请输入课程描述"
        ></el-input>
      </el-form-item>

      <el-form-item label="教学目标">
        <div v-for="(goal, index) in taskForm.goals" :key="index" class="goal-item">
          <el-input v-model="taskForm.goals[index]" placeholder="请输入教学目标" class="goal-input">
            <template #append>
              <el-button @click="removeGoal(index)" :disabled="taskForm.goals.length <= 1">
                <el-icon><Delete /></el-icon>
              </el-button>
            </template>
          </el-input>
        </div>
        <el-button type="primary" plain @click="addGoal" class="add-goal-btn">
          <el-icon><Plus /></el-icon>添加教学目标
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="createTask">创建任务</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Delete, Plus } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const taskForm = ref({
  title: '',
  subject: '',
  grade: '',
  description: '',
  goals: [''],
})

const addGoal = () => {
  taskForm.value.goals.push('')
}

const removeGoal = (index: number) => {
  taskForm.value.goals.splice(index, 1)
}

const createTask = () => {
  // 这里添加表单验证和提交逻辑
  if (!taskForm.value.title || !taskForm.value.subject || !taskForm.value.grade) {
    ElMessage.warning('请填写必填项')
    return
  }

  // 模拟创建成功
  ElMessage.success('创建成功')
  router.push('/teaching-design')
}

const resetForm = () => {
  taskForm.value = {
    title: '',
    subject: '',
    grade: '',
    description: '',
    goals: [''],
  }
}
</script>

<style scoped>
.task-create-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.page-header {
  margin-bottom: 30px;
}

.page-header h1 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.task-form {
  max-width: 800px;
}

.goal-item {
  margin-bottom: 10px;
}

.goal-input {
  width: 100%;
}

.add-goal-btn {
  width: 100%;
  margin-top: 10px;
}
</style>
